<template>
  <div class="settWrap">
    <div class="wrapCont">
      <div class="header">
        <ul class="clearfix">
          <li v-for="item in statusList" :ref="item.class" @click='onStatus(item.status)' :key="item.status">
            <p :class="{'active': statusActive == item.status}">{{item.txt}}<span v-show="item.status != '0'">({{item.count}})</span></p>
            <i v-show="statusActive == item.status"></i>
          </li>
        </ul>
      </div>
      <swiper :options="swiperOption" ref="mySwiper">
        <swiper-slide  v-for="item in statusList" :key="item.status" >
            <div class="main-body" ref="wrapper" :style="{'height':wrapperHeight+'px'}">
              <mt-loadmore class="mainChi" :top-method="loadTop" @top-status-change="handleTopChange" :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" :bottomPullText='bottomText' ref="loadmore">
                <mt-swiper-slide-cont :child-list="list[item.status]"></mt-swiper-slide-cont>
                <div slot="top" class="mint-loadmore-top">
                    <span v-show="topStatus !== 'loading'" :class="{ 'rotate': topStatus === 'drop' }">↓</span>
                    <span v-show="topStatus === 'loading'">正在刷新...</span>
                </div>
              </mt-loadmore>
              <div class="mailingAddress" v-show="item.status == 35">
                <!-- v-show="item.status == 35" -->
                <span>注：请将结算单号写在发票备注栏</span>
              </div>
            </div>
        </swiper-slide>
      </swiper>
      <!-- <div class="contant" v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
        <div class="offerList" v-for="(item, index) in list" :key="index">
          <div class="singerBox">
            <div class="boxTitle clearfix">
              <div class="left fl">结算单号：{{item.settleId}}</div>
              <div class="right fr">{{item.settleStatusName}}</div>
            </div>
            <div class="boxTitle clearfix">
              <div class="left fl">发票抬头：</div>
              <div class="left fl name">上海船奇物流有限公司</div>
            </div>
            <div class="border">
              <i class="halfLe"></i>
              <i class="halfRi"></i>
            </div>
            <div class="info">
              <div class="up clearfix">
                <div class="left fl">结算量：{{(item.totSettleWeight*1).toFixed(3)}}吨</div>
                <div class="right fr">运费总额：{{(item.totTransAmount*1).toFixed(2)}}元</div>
              </div>
              <div class="down clearfix">
                <div class="right fl">杂费总额：{{(item.totOtherAmount*1).toFixed(2)}}元</div>
                <div class="right fr">结算总额：<i>{{(item.totSettleAmount*1).toFixed(2)}}<i>元</i></i></div>
              </div>
            </div>
            <div class="clearfix dispatchBtnWrap">
              <span class="fl" @click='toDetail(item.settleId)'>查看详情</span>
              <mt-button class='btn needsclick fr' type="primary" size="large"
                         @click.native='toInvoicing(item.settleId,item.settleStatus)'
                         v-show="isShow(item.settleStatus)">{{item.settleStatus | settleStatusBtn}}
              </mt-button>
              <div class="fr date" style="color: #999;font-size:13px;">{{item.settleDate}}</div>
            </div>
          </div>
        </div>
        <div class="more_loading">
          <div v-show="showLoading">
            <mt-spinner type="snake" color="#00ccff" :size="10"></mt-spinner>
            <span>正在加载</span>
          </div>
          <span v-show="showALlLoad">已全部加载</span>
        </div>
      </div> -->
    </div>
    <m-tabbar :child-msg="msg"></m-tabbar>
    <!-- 筛选 -->
    <mt-popup v-model="searchPopup" popup-transition="popup-fade">
      <div class="searchWrap">
        <div class="head">
          <h3>筛选</h3>
          <span @click='closeSearchPopup'>×</span>
        </div>
        <div class="searCont">
          <ul class="list">
            <li>
              <p>结算单号</p>
              <div class="iptDiv">
                <input type="text" v-model="settleId" placeholder="请输入结算单号">
              </div>
            </li>
            <li>
              <p>运单号</p>
              <div class="iptDiv">
                <input type="text" v-model="voucherNum" placeholder="请输入运单号">
              </div>
            </li>
            <li>
              <p>时间区间</p>
              <div class="iptDiv timeGroup">
                <input type="text" placeholder="" v-model='beginTime' @click="openPicker(0)">
                <span>--</span>
                <input type="text" placeholder="" v-model='endTime' @click="openPicker(1)">
              </div>
            </li>
            <li class="status">
              <p>发票号</p>
              <div class="iptDiv">
                <input type="text" v-model="invoiceNum" placeholder="请输入发票号">
              </div>
            </li>
          </ul>
        </div>
        <div class="foot clearfix">
          <a href="javascript:;" class="fl"  @click="toReset">重置</a>
          <a href="javascript:;" class="fr" @click="tosearch">完成</a>
        </div>
      </div>
    </mt-popup>
    <!-- 底部打开日期 -->
    <mt-datetime-picker
      ref="startPicker"
      type="datetime"
      v-model="pickerValue"
      @confirm="start">
    </mt-datetime-picker>
  </div>
</template>
<script type="text/ecmascript-6">
  import Vue from 'vue'
  import ApiService from '@/services/API-servies'
  import {
    Tabbar,
    TabItem,
    Cell,
    TabContainer,
    TabContainerItem,
    Button,
    InfiniteScroll,
    Indicator,
    Spinner,
    Popup,
    DatetimePicker,
    Loadmore
  } from 'mint-ui';
  import MTabbar from '../foot/Tabbarfoot'
  import CountDown from '../public/public.vue'
  import { swiper, swiperSlide } from 'vue-awesome-swiper'
  import 'swiper/dist/css/swiper.css'
  import MtSwiperSlideCont from './MtSwiperSlideCont'

  Vue.use(InfiniteScroll);
  export default {
    data() {
      return {
        statusList: [{
          status: '0',
          txt: '全部结算单',
          count:'',
        }, {
          status: '35',
          txt: '待开票',
          count:'0',
        }, {
          status: '65',
          txt: '已收款',
          count:'0',
        }, {
          status: '50',
          txt: '结算完成',
          count:'0',
        }],
        searchStatusActive: '',
        searchStatus: [{
          status: '',
          txt: '全部',
        }, {
          status: '',
          txt: '待开票',
        }, {
          status: '',
          txt: '结算完成',
        }],
        statusActive: '',
        loadList: [],
        session: '',
        setUserInfo2: '',
        timeTit: '',
        bidTime: '',
        bidTimeList: [],
        msg: 'settlement',
        list: {
          '0':[],
          '35':[],
          '65':[],
          '50':[],
        },
        sortFlog: true,//默认是按照正序价格排列
        priceChecked: 'all',//默认选中的价格范围
        busy: true,//默认允许滚动执行loadMore函数
        pageNo: 1,//默认分页为第一页
        pageSize: 10,//默认每页显示8条数据
        flag: false,//默认没有分页
        showLoading: true,
        showALlLoad: false,
        searchPopup: false,
        pickerValue: new Date(),
        timeType:'',
        beginTime: '',
        endTime: '',
        invoiceNum:"",
        voucherNum:"",
        settleId:"",
        allLoaded:false,
        wrapperHeight:0,
        topStatus:'',
        scrollMode:'auto',
        bottomText:'上拉加载更多...',
        swiperOption: {
          initialSlide:0,
          notNextTick: true,
          pagination: '.swiper-pagination',
          paginationClickable: true,
          mousewheelControl: true,
          observeParents: true,
            onSlideChangeEnd: swiper => {
              this.loadListFun(this.statusActive,false,true);
              switch (swiper.activeIndex) {
                case 0:
                  this.statusActive='0';
                  break;
                case 1:
                  this.statusActive='35'
                  break;
                case 2:
                  this.statusActive='65'
                  break;
                case 3:
                  this.statusActive='50'
                  break;
                default:
                  break;
              }
              localStorage.setItem('localSettleStatus',this.statusActive);
          },
        },
      }
    },
    created() {
      window.clickSearch = this.clickSearch;
      if(localStorage.getItem('localSettleStatus')){
        this.statusActive = localStorage.getItem('localSettleStatus');
        let index = this.getIndex();
        this.swiperOption.initialSlide=index;
      };
      // this.loadListFun();
    },
    mounted() {
      setTimeout(() => {
        if (this.$isBrower.isAndroid) {
          this.getAndWay();
          this.setAndTitleType();
        } else if (this.$isBrower.isIos) {
          this.setIosTitleType();
          this.iosSearch();
        }
      }, 0);
      for(let i in this.statusList){
        this.loadListFun(this.statusList[i].status);
        this.queryCount(this.statusList[i]);
      };
      this.statusActive = localStorage.getItem('localSettleStatus');
    },
    methods: {
      getIndex(){
        let index;
        switch (this.statusActive) {
          case '0':
            index=0
            break;
          case '35':
            index=1
            break;
          case '65':
            index=2
            break;
          case '50':
            index=3
            break;
          default:
            break;
        }
        return index
      },
      loadTop() {
        this.pageNo = 1;
        this.on=true;
        this.loadListFun(this.statusActive,false,true);
      },
    loadBottom() {
        this.pageNo++;
        this.loadListFun(this.statusActive,true);

    },
    handleTopChange(status) {
      this.topStatus = status;
    }
    ,
    queryCount(_statusObj){
       ApiService.settle.waybills({
          status: _statusObj.status
        }).then((res) => {
          if (res.meta.retCode === '0') {
            _statusObj.count = res.data;
          }
      });

    },
      loadListFun(status,flag,on) {
        if(!flag){
          this.pageNo = 1;
        }
        let _this = this;
        let sta = status;
        this.statusActive = status;
        let staAc = this.statusActive;
        this.showLoading = true;
        this.showALlLoad = false;
        // if(this.statusActive == 0) this.statusActive='';
        if(status == 65 || status== 0){
          sta = ''
        }
        if(staAc == 65){
          staAc = '40'
        }else{
          staAc = ''
        }
        ApiService.settle.load({
          pageSize: this.pageSize,
          pageNo: this.pageNo,
          source:"app",
          // settleStatus: this.statusActive=="65"?"":this.statusActive,
          settleStatus: sta,
          payFlag: staAc,
          // payFlag: this.statusActive=="65"?"40":"",
          type: "1",
          endTime: this.endTime,
          startTime: this.beginTime,
          invoiceNum: this.invoiceNum,
          voucherNum: this.voucherNum,
          settleId: this.settleId
        }).then((res) => {
          this.showLoading = true;
          let index = this.getIndex();
          if (flag) {//判断是否通过分页
            this.list[status] = this.list[status].concat(res.data.list);//分页数据追加到这个list里面
            if (res.data.list.length < 10) {//判断数是否加载完成，就让数据截停
              this.busy = true;
            } else {
              this.busy = false;
            }
            for(let i=0;i<4;i++){
              this.$refs.loadmore[i].onBottomLoaded();
            }
          } else {
            this.list[status] = res.data.list;
            this.busy = false;
            if (res.data.list.length < 10) {//判断数是否加载完成，就让数据截停
              this.busy = true;
            };
            if(on){
              for(let i=0;i<4;i++){
                _this.$refs.loadmore[i].onTopLoaded();
              }
            }
          }
          this.wrapperHeight = document.documentElement.clientHeight - 112;
        });
      },
      loadMore: function () {
        this.busy = true;
        setTimeout(() => {
          this.pageNo++;
          this.loadListFun(true);
        }, 500)
      },
      onStatus(re) {
        this.statusActive = re;
        localStorage.setItem('localSettleStatus',this.statusActive);
        // this.loadListFun();
        let index = this.getIndex();
        // this.swiperOption.initialSlide=index;
        this.swiper.activeIndex = index;
        this.loadListFun(this.statusActive,false,true);
      },
      tosearch() {
        this.pageNo = 1;
        this.loadListFun(this.statusActive,false,true);
        this.searchPopup = false;
        this.swiper.activeIndex = 0;
        this.statusActive = '0';
      },
      toReset() {
        this.beginTime = '';
        this.endTime = '';
        this.invoiceNum = '';
        this.voucherNum = '';
        this.settleId = '';
        this.statusActive='0';
        this.swiper.activeIndex = 0
      },closeSearchPopup(){
         this.toReset();
         this.searchPopup = false;
      },
      searchOnStatus(index) {
        this.searchStatusActive = index;
      },
      toDetail(settleId) {
        this.$router.push({path: 'settlementDetail', query: {settleId: settleId}})
      },
      toInvoicing(settleId, _status) {
        this.$router.push({path: 'invoicing', query: {settleId: settleId}})
      },
      getAndWay() {
          app.setWaybill();
      },
      setAndTitleType() {
          app.setTitleType({
            isReturn: false,
            isSearch: true
          });
      },
      isShow(val) {
        if (val == 35 || val == 60) {
          return true;
        }
        return false;
      },
      arrived(transBillId) {
        ApiService.transport.arrived({
          transBillId: transBillId
        }).then((res) => {
          if (res.meta.retCode === '0') {
            return;
          }
        });
      },
      openPicker(type) {
        this.timeType = type;
        this.$refs.startPicker.open();
      },
      start(value) {
        let start = this.$moment(value).format("YYYY-MM-DD HH:mm");
        if(this.timeType == 0){
          this.beginTime = start;
        }else if(this.timeType == 1){
          this.endTime = start;
        }
      },
      // andriod 搜索方法
      clickSearch() {
        this.searchPopup = true;
      },
      //ios 搜索方法
      iosSearch() {
        this.$bridge.registerhandler('searchPopup', (data) => {
          this.searchPopup = true;
        })
      },
      //ios 设置头部信息
      setIosTitleType(){
        let params = {
          isReturn: false,
          isSearch: true,
          isTitleSegment:false
        };
        this.$bridge.callhandler('needBarButton', params, (data) => {})
      }
    },
    filters: {
      status(val) {
        if (val == 30) {
          return '待竞价'
        } else if (val == 40) {
          return '参与竞价'
        }
      },
      city(val) {
        var index = val.indexOf('-') + 1;
        return val.substring(index)
      },
      settleStatusBtn(val) {
        if (val == 30) {
          return '发票上传'
        } else if (val == 50) {
          return '发票上传'
        } else if (val == 35 || val == 40) {
          return '发票上传'
        }
      }
    },
    computed: {
      swiper() {
        return this.$refs.mySwiper.swiper
      }
    },
    components: {
      MtTabbar: Tabbar,
      MtTabItem: TabItem,
      mtCell: Cell,
      MtTabContainer: TabContainer,
      MtTabContainerItem: TabContainerItem,
      MTabbar,
      MtButton: Button,
      CountDown,
      MtSpinner: Spinner,
      MtPopup: Popup,
      MtDatetimePicker: DatetimePicker,
      MtLoadmore: Loadmore,
      swiper,
      swiperSlide,
      MtSwiperSlideCont
    }
  }
</script>

<style lang="less" rel="stylesheet/less">
  @import "~common/less/variable";
  .settWrap {
    .wrapCont {
      padding: 0px 0 120px;
      .main-body{
        overflow: scroll;
        -webkit-overflow-scrolling:touch;
        margin-top: 120px;
        .mainChi{
          padding-bottom: 40px;
        }
      }
      .header {
        span{
                color: red
              }
        position: fixed;
        width: 100%;
        z-index: 99;
        background: #fff;
        padding-top: 20px;
        margin-bottom: 10px;
        box-shadow: 0px 4px 10px 0px rgba(183,186,199,0.5);
        ul {
          li {
            float: left;
            width: 25%;
            text-align: center;
            &.driverLi {
              width: 33.33%;
            }
            p {
              color: #5C6063;
              margin-bottom: 10px;
              &.active {
                color: @blue
              }
            }
            i {
              display: block;
              margin: 0 auto;
              width: 60%;
              height: 6px;
              background: @blue;
            }
          }
        }
      }
      .contant{
        padding-top: 120px;
      }
      .offerList {
        font-size: 28px;
        padding: 10px 20px;
        .singerBox {
          background: #fff;
          padding: 30px;
          padding-bottom: 10px;
          border-radius: 16px;
          box-shadow: 0px 4px 10px 0px rgba(183,186,199,0.5);
          .boxTitle {
            margin-bottom: 12px;
            .left {
              line-height: 36px;
              color: #999;
              &.name{
                // color: #333;
              }
            }
            .right {
              background: #5395FF;
              color: #fff;
              padding: 8px 12px;
              border-radius: 200px;
              font-size: 24px;
              font-weight: 400;
              line-height: 28px
            }
            .date{
              color: #999;
              line-height: 36px;
            }
          }
          .address {
            font-size: 32px;
            font-weight: 800;
            position: relative;
            & span {
              font-weight: 500
            }
            .iconTo {
              display: inline-block;
              width: 60px;
              position: absolute;
              left: 50%;
              background: url(../../common/imgs/iconTo.png) no-repeat center;
              background-size: 100%;
              transform: translateX(-50px)
            }
            .add {
              width: 267px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }
          .info {
            font-size: 26px;
            color: #666;
            margin-bottom: 6px;
            line-height: 40px;
            .down {
              margin-top: 10px;
              span {
                color: #FF0505
              }
              .fr i {
                color: #FF0505;
                font-weight: 400;
              }
            }
          }
          .border {
            border-top: 3px dashed #E8E7E8;
            height: 3px;
            margin: 20px 0;
            position: relative;
            .halfLe{
              display: block;
              position: absolute;
              left: -44px;
              top:-20px;
              width: 40px;
              height: 40px;
              background: url(../../common/imgs/harfLe.png) no-repeat center center;
              background-size: contain;
            }
            .halfRi{
              display: block;
              position: absolute;
              right: -44px;
              top:-20px;
              width: 40px;
              height: 40px;
              background: url(../../common/imgs/harfRi.png) no-repeat center center;
              background-size: contain;
            }
          }
          .dispatchBtnWrap {
            text-align: right;
            .fl {
              color: @blue;
              font-size: 26px;
              line-height: 60px;
            }
            .btn {
              width: 180px;
              font-size: 28px;
              line-height: 30px;
              height: 52px;
              border-radius: 8px;
              background: @blue
            }
          }
        }
      }
      .more_loading {
        text-align: center;
        .mint-spinner-snake {
          display: inline-block;
          vertical-align: middle;
          margin-right: 10px
        }
      }
    }
    .searchWrap {
      width: 660px;
      text-align: center;
      .head {
        h3 {
          color: #F9F3F3;
          line-height: 88px;
          font-size: 36px;
          background: @blue;
        }
        span {
          position: absolute;
          top: 26px;
          right: 20px;
          width: 56px;
          height: 56px;
          line-height: 48px;
          font-size: 56px;
          color: #F9F3F3
        }
      }
      .searCont {
        background: #fff;
        max-height: 560px;
        overflow:auto;
        .list {
          margin: 0 30px;
          text-align: left;
          p {
            font-size: 28px;
            color: #333;
            font-weight: 400;
          }
          .iptDiv {
            background: #F0F2F5;
            padding: 14px;
            line-height: 10px;
            input {
              color: #333;
              font-size: 26px;
              height: 56px;
              width: 99%;
            }
            &.timeGroup {
              span {
                color: #C1C1C1;
                font-weight: 800
              }
              input {
                width: 254px;
              }
            }
          }
          .status {
            li {
              position: relative;
              float: left;
              color: #999;
              font-size: 26px;
              padding: 4px 30px;
              background: #F0F2F5;
              margin-right: 18px;
              margin-bottom: 20px;
              display: inline-block;
              border-radius: 8px;
              border: 2px solid #F0F2F5;
              &:nth-child(4), &:nth-child(7) {
                margin-right: 0;
              }
              &.active {
                border: 2px solid #2F7AF3;
                color: #2F7AF3
              }
              &.active:after {
                content: "";
                position: absolute;
                bottom: 0;
                right: 0;
                width: 36px;
                height: 32px;
                background: url(../../common/imgs/check.png) no-repeat 0 0;
                background-size: 100%;
              }
            }
          }
        }
      }
      .foot {
        border-top: 2px solid #D8D8D8;
        padding: 10px 20px 0;
        margin: 0;
        a {
          display: block;
          width: 300px;
          height: 80px;
          background: @blue;
          color: #fff;
          font-weight: 400;
          font-size: 32px;
          line-height: 80px;
          text-align: center;
          border-radius: 8px;
          margin: 30px auto;
        }
      }
    }
    .clickH5Btn2 {
      margin-top: 20px
    }
    .mailingAddress{
        position: fixed;
        bottom: 0;
        width: 100%;
        background: #fff6d2;
        color: #000;
        text-align: center;
        font-size: 28px;
        line-height: 60px;
    }
  }
  .mint-indicator-wrapper{
    z-index: 999
  }
</style>
